//
//  TestListView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2022/3/28.
//  Copyright © 2022 moon. All rights reserved.
//

#import "TestListView.h"
#import "TestFunButton.h"
#import "Menu.h"

@interface TestListMenuButton : MKUIFlowLayoutButton<MKUIListCellViewProtocol>
@property(nonatomic,strong) Menu *menuObj;
- (id)initWithMenu:(Menu *)menu;
@end
@interface TestListExpendCell : MKUIListPinHeadViewBase{
    struct{
        CGPoint beginPoint;
        CGPoint endPoint;
        MKCGRange heightRange;
        BOOL isExpendingGesture;
        CGFloat expendingHeight;
        CGVector vector;
    } __gestureStateOfUIPanGestureRecognizer;
}
@property(nonatomic,strong) MKUIListView *topMenusList;
@property(nonatomic,strong) NSArray<Menu *> *menus;
@property(nonatomic,strong) MKUIFlowLayoutButton *expendButton;
@property(nonatomic,strong) UIView *expendGestureView;
@property(nonatomic,strong) MKUISegmentFlowLayoutConstraint *flowlayout;
@property(nonatomic,assign) CGFloat topMenuListMinScale;
@property(nonatomic,strong) UIPanGestureRecognizer *expendGesture;
- (void)reloadData;
@end

@interface TestListViewTextCellView : MKUIListSelectionCellViewBase<MKUIListCellViewProtocol,MKUIListSectionViewProtocol>
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@property(nonatomic,assign) BOOL expend;
@property(nonatomic,assign) BOOL showBorder;
@property(nonatomic,strong) MKUIWaterFlowLayoutConstraint *buttonsLayout;
- (void)addButtonWithTitle:(NSString *)title action:(void(^)(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button))action;
@property(nonatomic,assign) BOOL showLog;
@end

@interface TestListSeparatorView : UIView
@property(nonatomic,strong) UIView *lineView;
@property(nonatomic,assign) UIEdgeInsets contentInsts;
@end


#pragma mark - -----------------------------
@implementation TestListMenuButton
- (id)initWithMenu:(Menu *)menu{
    if (self=[self initWithContentStyle:(MKUIFlowLayoutButtonContentStyleVertical)]) {
        self.menuObj = menu;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self setTitleColor:[UIColor mk_colorWithLight:[UIColor blackColor]] forState:(UIControlStateNormal)];
        self.imageSize = CGSizeMake(44, 44);
        self.interitemSpacing = 5;
        self.contentInsets = MKUIEdgeInsetsMakeSameEdge(5);
        [self _reloadData];
        [self mk_addClickActionBlock:^(TestListMenuButton *self) {
            NSLog(@"click menu:%@",self.menuObj);
        } context:self];
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1;
    }
    return self;
}
- (void)_reloadData{
    [self setTitle:self.menuObj.title forState:(UIControlStateNormal)];
    [self setImage:self.menuObj.icon forState:(UIControlStateNormal)];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return [super sizeThatFits:size];
}
- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
}
@end
@implementation TestListExpendCell
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.topMenuListMinScale = 0.3;
        self.topMenusList = [[MKUIListView alloc] init];
        self.topMenusList.scrollDirection = MKUIListViewScrollDirectionHorizontal;
        self.topMenusList.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
        self.topMenusList.separatorColor = [UIColor clearColor];//实现宽的空白间隔
        self.topMenusList.separatorInsets = UIEdgeInsetsZero;
        self.topMenusList.separatorSize = 10;
        
        [self.contentView addSubview:self.topMenusList];
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.topMenusList sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            MKUIListView *topMenusList = wrapper.originItem;
            CGSize s = [topMenusList sizeThatFits:CGSizeMake(999999, 80)];
            return s;
        }];
        //
        self.expendGestureView = [[UIView alloc] init];
        [self.contentView addSubview:self.expendGestureView];
        self.expendGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_onExpendGesture:)];
        [self.expendGestureView addGestureRecognizer:self.expendGesture];
        //
        self.expendButton = [[TestFunButton alloc] init];
        [self.expendButton setTitleColor:[UIColor mk_colorWithLight:[UIColor blackColor]] forState:(UIControlStateNormal)];
        [self.expendButton setTitle:@"展开" forState:(UIControlStateNormal)];
        [self.expendButton setTitle:@"收起" forState:(UIControlStateSelected)];
        [self.expendButton mk_addClickActionBlock:^(TestListExpendCell *self) {
            self.expendButton.selected = !self.expendButton.selected;
            [self _expendCell:self.expendButton.selected];
        } context:self];
        [self.contentView addSubview:self.expendButton];
        //
        self.flowlayout = [[MKUISegmentFlowLayoutConstraint alloc] initWithItems:@[w1,self.expendButton] constraintParam:(MKUISegmentFlowLayoutConstraint_V_C) contentInsets:(UIEdgeInsetsMake(10, 0, 5, 0)) interitemSpacing:5];
        self.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
        
    }
    return self;
}
- (void)_onExpendGesture:(UIPanGestureRecognizer *)gesture{
    UIView *view = [UIApplication sharedApplication].keyWindow;
    MKUIListView *listView = [self mk_listView];
    CGRect bounds = listView.bounds;
    CGPoint p = [gesture translationInView:view];
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            memset(&__gestureStateOfUIPanGestureRecognizer, 0, sizeof(__gestureStateOfUIPanGestureRecognizer));
            __gestureStateOfUIPanGestureRecognizer.beginPoint = p;
            __gestureStateOfUIPanGestureRecognizer.endPoint = p;
            __gestureStateOfUIPanGestureRecognizer.isExpendingGesture = YES;
            __gestureStateOfUIPanGestureRecognizer.heightRange.begin = self.bounds.size.height;
            if(self.expendButton.selected){
                __gestureStateOfUIPanGestureRecognizer.heightRange.end = [self.flowlayout sizeThatFits:CGSizeMake(bounds.size.width, 999999) resizeItems:YES].height;
            }else{
                __gestureStateOfUIPanGestureRecognizer.heightRange.end = bounds.size.height;
            }
            __gestureStateOfUIPanGestureRecognizer.expendingHeight = __gestureStateOfUIPanGestureRecognizer.heightRange.begin;
            break;
        case UIGestureRecognizerStateChanged:{
            CGPoint p1 = __gestureStateOfUIPanGestureRecognizer.endPoint;
            CGPoint p2 = p;
            CGFloat dis = p.y-__gestureStateOfUIPanGestureRecognizer.beginPoint.y;
            __gestureStateOfUIPanGestureRecognizer.vector = CGVectorMake(p2.x-p1.x, p2.y-p1.y);
            CGFloat progress = dis/(MKCGRangeGetLength(__gestureStateOfUIPanGestureRecognizer.heightRange));
            progress = MAX(progress,0);
            progress = MIN(progress,1);
            __gestureStateOfUIPanGestureRecognizer.expendingHeight = MKCGRangeInterpolate(__gestureStateOfUIPanGestureRecognizer.heightRange, progress);
            [listView reloadView:self withAnimation:(MKUIListViewItemAnimationNone)];
            __gestureStateOfUIPanGestureRecognizer.endPoint = p;
        }
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:{
            CGFloat v = __gestureStateOfUIPanGestureRecognizer.vector.dy;
            memset(&__gestureStateOfUIPanGestureRecognizer, 0, sizeof(__gestureStateOfUIPanGestureRecognizer));
            BOOL expend = v>=0;
            [self _expendCell:expend];
        }
        default:
            break;
    }
}
- (void)_expendCell:(BOOL)expend{
    self.expendButton.selected = expend;
    MKUIListView *listView = [self mk_listView];
    listView.scrollEnabled = !expend;
    [listView performBatchUpdates:^{
        [listView reloadView:self withAnimation:(MKUIListViewItemAnimationAutomatic)];
    } animated:YES completion:^(BOOL finished) {
        [listView scrollToView:self atScrollPosition:(MKUIListViewScrollPositionHead) animated:YES completion:^(BOOL finished) {
            [self listViewDidChangeContentOffset:listView];
        }];
    }];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    {
        CGRect f1 = self.expendButton.mk_frameSafety;
        f1.size.width = bounds.size.width;
        f1.origin.x = 0;
        self.expendGestureView.mk_frameSafety = f1;
    }
}
- (CGSize)sizeThatFits:(CGSize)size{
    if(__gestureStateOfUIPanGestureRecognizer.isExpendingGesture){//展开/收起手势时，会时时修改size
        return CGSizeMake(size.width, __gestureStateOfUIPanGestureRecognizer.expendingHeight);
    }else{
        if(self.expendButton.selected){
            if(self.mk_listView){
                CGRect bounds = self.mk_listView.bounds;
                return CGSizeMake(size.width, bounds.size.height-MKUIEdgeInsetsGetEdgeSum(self.mk_listView.mk_adjustedContentInset, MKCGAxisY));
            }else{
                return size;
            }
        }else{
            return [self.flowlayout sizeThatFits:CGSizeMake(size.width, 999999) resizeItems:YES];
        }
    }
}
- (void)reloadData{
    [self.topMenusList deleteAllSectionsWithAnimation:(MKUIListViewItemAnimationAutomatic)];
    [self.topMenusList addSection];
    @MK_WEAKIFY(self);
    for (Menu *menu in self.menus) {
        TestListMenuButton *button = [[TestListMenuButton alloc] initWithMenu:menu];
        [button mk_addClickActionBlock:^(TestListExpendCell *button) {
            @MK_NORMALIZE(self);
            [button.mk_listView performBatchUpdates:^{
                [button.mk_listView deleteItem:button withAnimation:(MKUIListViewItemAnimationAutomatic)];
            } animated:YES completion:^(BOOL finished) {
                if(self.topMenusList.numberOfItems==0){
                    [self.mk_listView deleteView:self withAnimation:(MKUIListViewItemAnimationAutomatic)];
                }else{
                    [UIView animateWithDuration:0.25 animations:^{
                        [self setNeedsLayout];
                        [self layoutIfNeeded];
                    }];
                }
            }];
        } context:button];
        [self.topMenusList addItem:button withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }
    [self.topMenusList reloadData];
}
- (CGFloat)minSizeOfPinning{
    CGFloat maxChangeHeight = self.topMenusList.bounds.size.height*(1-self.topMenuListMinScale);
    return self.bounds.size.height-maxChangeHeight;
}
- (CGFloat)topMargginOfPinning{
    CGFloat topMarggin = -(self.topMenusList.bounds.size.height*(1-self.topMenuListMinScale));
    //查找listView上之前的pin
//    MKUIListView *listView = self.mk_listView;
//    NSIndexPath *indexpath = [listView indexPathForCell:self];
//    NSArray<UIView *> *beforeViews = [listView viewsBeforeItemAtIndexPath:indexpath];
//    for(UIView *v in beforeViews){
//        if([v isKindOfClass:MKUIListPinHeadViewBase.class]){
//            MKUIListPinHeadViewBase *other = (MKUIListPinHeadViewBase *)v;
//            topMarggin += other.bounds.size.height;
//        }
//    }
    return topMarggin;
}
- (void)listViewDidChangeContentOffset:(MKUIListView *)listView{
    [super listViewDidChangeContentOffset:listView];
    MKUIListViewAttributeContainerView *containerView = [listView containerViewForView:self];
    UIEdgeInsets contentInsets = listView.mk_adjustedContentInset;
    CGPoint offset = listView.contentOffset;
    CGRect f1 = [containerView.superview convertRect:containerView.mk_frameSafety toView:listView];
    //
    CGFloat y0 = f1.origin.y;
    CGFloat y1 = offset.y;
    CGFloat topMarrgin = 0;//pin视图与listview顶部的间隔
    topMarrgin += contentInsets.top;
    CGFloat p = 0;
    CGFloat dy = y0-y1-topMarrgin;
    CGFloat minPinViewHeight = [self minSizeOfPinning];
    
    if(dy<0){
        p = -(dy)/(self.bounds.size.height-minPinViewHeight);
        p = MIN(1,MAX(0,p));
    }
    //
    {
        //topMenuList缩小且位于底部
        CGRect f2 = self.topMenusList.bounds;
        CGFloat scale = MKCGFloatInterpolate(1, self.topMenuListMinScale, p);
        
        CGAffineTransform m2 = CGAffineTransformIdentity;
        m2 = CGAffineTransformConcat(m2, CGAffineTransformMakeScale(scale, scale));
        m2 = CGAffineTransformConcat(m2, CGAffineTransformMakeTranslation(0, (1-scale)*f2.size.height*0.5));
        self.topMenusList.transform = m2;
    }
    //渐变隐藏button的title
    CGFloat alpha = MKCGRangeInterpolate(MKCGRangeMake(1, 0), p);
    for (int i=0; i<self.menus.count; i++) {
        TestListMenuButton *button = [self.topMenusList cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        button.titleLabel.alpha = alpha;
    }
    self.expendButton.alpha = alpha;
    self.expendGesture.enabled = p==0;
}
@end

@implementation TestListViewTextCellView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.enableSelection = YES;
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.titleLabel.textColor = [UIColor mk_colorWithLight:[UIColor blackColor]];
        [self.contentView addSubview:self.titleLabel];
        //
        self.buttonsLayout = [[MKUIWaterFlowLayoutConstraint alloc] initWithItems:@[] constraintParam:(MKUIWaterFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:5 lineSpacing:5];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.buttonsLayout,self.titleLabel] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:MKUIEdgeInsetsMakeSameEdge(5) interitemSpacing:5];
        //
        self.selectedBackgroundColor = [UIColor mk_listViewCellSelectedColor];
        self.backgroundColor = UIColor.lightTextColor.mk_autoDynamicDarkColor;
//        self.backgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.2];
        self.whenSelected = ^(TestListViewTextCellView *self, BOOL selected) {
            NSLog(@"select:%@,selected:%@",self,@(selected));
        };
        self.contentView.layer.borderColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.5].CGColor;
        self.contentView.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
    }
    return self;
}
- (void)listView:(MKUIListView *)listView didSelectAtIndexPath:(NSIndexPath *)indexPath{
    @MK_WEAKIFY(self);
    [self setSelected:YES animated:YES completion:^(BOOL finished) {
        @MK_NORMALIZE(self);
        TestListView *vc = [[TestListView alloc] init];
        [self.mk_navigationControllerOfFirst pushViewController:vc animated:YES];
    }];
}
- (id)initWithTitle:(NSString *)title{
    if (self=[self init]) {
        self.titleLabel.text = title;
    }
    return self;
}
- (void)setShowBorder:(BOOL)showBorder{
    _showBorder = showBorder;
    if(_showBorder){
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1/[UIScreen mainScreen].scale;
    }else{
        self.layer.borderWidth = 0;
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:CGSizeMake(size.width, 99999) resizeItems:YES];
    if(self.expend){
        s.height += 50;
    }else{
        s.height = MIN(s.height,100);
    }
    return s;
}
- (void)addButtonWithTitle:(NSString *)title action:(void(^)(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button))action{
    TestFunButton *button = [[TestFunButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor].mk_autoDynamicDarkColor forState:(UIControlStateNormal)];
    @MK_WEAKIFY(button);
    [button mk_addClickActionBlock:^(TestListViewTextCellView *self) {
        @MK_NORMALIZE(button);
        if(action){
            action(self,button);
        }
    } context:self];
    [self.contentView addSubview:button];
    [self.buttonsLayout addItem:button];
}

- (CGSize)listView:(MKUIListView *)listView sizeThatFits:(CGSize)size atIndexPath:(NSIndexPath *)indexPath{
    return [self sizeThatFits:size];
}

- (void)listView:(MKUIListView *)listView didAddAtIndexPath:(NSIndexPath *)indexPath{
    if(!self.showLog)return;
    NSLog(@"didAddAtIndexPath:%@",indexPath);
}
- (void)listView:(MKUIListView *)listView didDeleteAtIndexPath:(NSIndexPath *)indexPath{
    if(!self.showLog)return;
    NSLog(@"didDeleteAtIndexPath:%@",indexPath);
}

- (void)listView:(MKUIListView *)listView willDisplayAtIndexPath:(NSIndexPath *)indexPath{
    if(!self.showLog)return;
    NSLog(@"willDisplayAtIndexPath:%@",indexPath);
}
- (void)listView:(MKUIListView *)listView didEndDisplayingAtIndexPath:(NSIndexPath *)indexPath{
    if(!self.showLog)return;
    NSLog(@"didEndDisplayingAtIndexPath:%@",indexPath);
}


- (void)listView:(MKUIListView *)listView didAddAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    if(!self.showLog)return;
    NSLog(@"didAddAtSection:%@,%@",@(section),@(kind));
}
- (void)listView:(MKUIListView *)listView didDeleteAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    if(!self.showLog)return;
    NSLog(@"didDeleteAtSection:%@,%@",@(section),@(kind));
}

- (void)listView:(MKUIListView *)listView willDisplayAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    if(!self.showLog)return;
    NSLog(@"willDisplayAtSection:%@,%@",@(section),@(kind));
}
- (void)listView:(MKUIListView *)listView didEndDisplayingAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    if(!self.showLog)return;
    NSLog(@"didEndDisplayingAtSection:%@,%@",@(section),@(kind));
}
@end

@implementation TestListSeparatorView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.lineView = [[UIView alloc] init];
        [self addSubview:self.lineView];
        self.lineView.backgroundColor = [UIColor grayColor];
        self.contentInsts = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    UIEdgeInsets insets = self.contentInsts;
    CGRect f1 = UIEdgeInsetsInsetRect(bounds, insets);
    self.lineView.frame = f1;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 10);
}
@end


@interface TestListView ()
@property(nonatomic,strong) MKUIListView *listView;
@property(nonatomic,strong) UIView *lineView;
@property(nonatomic,strong) TestListViewTextCellView *listHeadView;
@property(nonatomic,strong) TestListViewTextCellView *listFootView;
@property(nonatomic,strong) UIView *listBackgroundView;
@end
@implementation TestListView

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"ListView" viewControllerClass:self];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor].mk_autoDynamicDarkColor;
    self.listView = [[MKUIListView alloc] initWithFrame:CGRectZero style:(MKUIListViewStylePlain)];
    self.listView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.listView.layer.borderColor = [UIColor redColor].CGColor;
    self.listView.layer.borderWidth = 1;
    self.listView.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
    self.listView.separatorBackgroundColor = [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.2].mk_autoDynamicDarkColor;
    self.listBackgroundView = [[UIView alloc] init];
    self.listBackgroundView.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0 alpha:0.1].mk_autoDynamicDarkColor;
    self.listView.bounces = YES;
    [self.view addSubview:self.listView];
    //
    self.lineView = [[UIView alloc] init];
    self.lineView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.lineView];
    //
    {
        TestListViewTextCellView *view = [[TestListViewTextCellView alloc] initWithTitle:@"This is list header"];
        view.showBorder = YES;
        view.backgroundColor = [UIColor grayColor];
        @MK_WEAKIFY(self);
        [view addButtonWithTitle:@"设置背景" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"移除背景" forState:(UIControlStateSelected)];
            BOOL selected = self.listView.backgroundView!=nil;
            button.selected = !selected;
            self.listView.backgroundView = button.selected?self.listBackgroundView:nil;
        }];
        [view addButtonWithTitle:@"自定义分隔线" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"默认分隔线" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            self.listView.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
            if(button.selected){
                self.listView.separatorViewClass = [TestListSeparatorView class];
            }else{
                self.listView.separatorViewClass = nil;
            }
        }];
        [view addButtonWithTitle:@"cell间隔0" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"cell间隔10" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                self.listView.cellInteritemSpacing = 10;
            }else{
                self.listView.cellInteritemSpacing = 0;
            }
            [self.listView reloadView:cellView];
        }];
        [view addButtonWithTitle:@"section间隔0" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"section间隔10" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                self.listView.sectionInteritemSpacing = 10;
            }else{
                self.listView.sectionInteritemSpacing = 0;
            }
            [self.listView reloadView:cellView];
        }];
        [view addButtonWithTitle:@"inset0" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"inset40" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                self.listView.contentInset = MKUIEdgeInsetsMakeSameEdge(40);
            }else{
                self.listView.contentInset = UIEdgeInsetsZero;
            }
            [self.listView reloadData];
        }];
        [view addButtonWithTitle:@"StylePlain" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"StyleGroup" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                self.listView.style = MKUIListViewStyleInsetGrouped;
            }else{
                self.listView.style = MKUIListViewStylePlain;
            }
        }];
        [view addButtonWithTitle:@"AdjustmentAutomatic" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"AdjustmentAlways" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                if (@available(iOS 11.0, *)) {
                    self.listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
                } else {
                    // Fallback on earlier versions
                }
            }else{
                if (@available(iOS 11.0, *)) {
                    self.listView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
                } else {
                    // Fallback on earlier versions
                }
            }
            [self.listView reloadData];
        }];
        [view addButtonWithTitle:@"移除分隔线" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.separatorStyle = MKUIListViewCellSeparatorStyleNone;
        }];
        [view addButtonWithTitle:@"移除Head" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.listHeaderView = nil;
        }];
        [view addButtonWithTitle:@"添加Foot" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.listFooterView = self.listFootView;
        }];
        [view addButtonWithTitle:@"清空所有" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [self.listView performBatchUpdates:^{
                self.listView.listHeaderView = nil;
                self.listView.listFooterView = nil;
                NSInteger numberOfSections = self.listView.numberOfSections;
                for (int i=0; i<numberOfSections; i++) {
                    [self.listView deleteSectionAtIndex:0 withAnimation:(MKUIListViewItemAnimationAutomatic)];
                }
            } animated:YES completion:nil];
        }];
        [view addButtonWithTitle:@"展开" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            cellView.expend = !cellView.expend;
            [button setTitle:@"收起" forState:(UIControlStateSelected)];
            button.selected = cellView.expend;
            CGRect f1 = cellView.frame;
            f1.size = [cellView sizeThatFits:CGSizeMake(f1.size.width, 9999)];
            [UIView animateWithDuration:0.25 animations:^{
                cellView.frame = f1;
                [self.listView reloadView:cellView withAnimation:(MKUIListViewItemAnimationNone)];
                [self.listView layoutIfNeeded];
            }];
        }];
        self.listHeadView = view;
    }
    {
        TestListViewTextCellView *view = [[TestListViewTextCellView alloc] initWithTitle:@"This is list footer"];
        view.showBorder = YES;
        @MK_WEAKIFY(self);
        view.backgroundColor = [UIColor grayColor];
        [view addButtonWithTitle:@"设置背景" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"移除背景" forState:(UIControlStateSelected)];
            BOOL selected = self.listView.backgroundView!=nil;
            button.selected = !selected;
            self.listView.backgroundView = button.selected?self.listBackgroundView:nil;
        }];
        [view addButtonWithTitle:@"自定义分隔线" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"默认分隔线" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            self.listView.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
            if(button.selected){
                self.listView.separatorViewClass = [TestListSeparatorView class];
            }else{
                self.listView.separatorViewClass = nil;
            }
        }];
        [view addButtonWithTitle:@"cell间隔0" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            [button setTitle:@"cell间隔100" forState:(UIControlStateSelected)];
            button.selected = !button.selected;
            if(button.selected){
                self.listView.cellInteritemSpacing = 100;
            }else{
                self.listView.cellInteritemSpacing = 0;
            }
            [self.listView reloadView:cellView];
        }];
        [view addButtonWithTitle:@"移除分隔线" action:^(TestListViewTextCellView *cellView, MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.separatorStyle = MKUIListViewCellSeparatorStyleNone;
        }];
        [view addButtonWithTitle:@"移除Foot" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.listFooterView = nil;
        }];
        [view addButtonWithTitle:@"添加Head" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            self.listView.listHeaderView = self.listHeadView;
        }];
        [view addButtonWithTitle:@"展开" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
            @MK_NORMALIZE(self);
            cellView.expend = !cellView.expend;
            [button setTitle:@"收起" forState:(UIControlStateSelected)];
            button.selected = cellView.expend;
            CGRect f1 = cellView.frame;
            f1.size = [cellView sizeThatFits:CGSizeMake(f1.size.width, 9999)];
            [UIView animateWithDuration:0.25 animations:^{
                cellView.frame = f1;
                [self.listView reloadView:cellView withAnimation:(MKUIListViewItemAnimationNone)];
                [self.listView layoutIfNeeded];
            }];
        }];
        self.listFootView = view;
    }
    //
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemRefresh) target:self action:@selector(__onRefreshForDeleteAndAdd)];
    [self __reloadData];
}
- (void)__onRefreshForDeleteAndAdd{
    UIView *headView = self.listView.listHeaderView;
    UIView *footView = self.listView.listFooterView;
    NSMutableArray *sections = [[NSMutableArray alloc] initWithCapacity:self.listView.numberOfSections];
    for(int i=0;i<self.listView.numberOfSections;i++){
        UIView *sectionHeadView = [self.listView sectionViewForSection:i kind:(MKUIListViewSectionViewKindOfHead)];
        UIView *sectionFootView = [self.listView sectionViewForSection:i kind:(MKUIListViewSectionViewKindOfFoot)];
        NSInteger cellCount = [self.listView numberOfItemsInSection:i];
        NSMutableArray *cells = [[NSMutableArray alloc] initWithCapacity:cellCount];
        for(int j=0;j<cellCount;j++){
            [cells addObject:[self.listView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:j inSection:i]]];
        }
        NSMutableDictionary *infos = [[NSMutableDictionary alloc] init];
        if(sectionHeadView) infos[@"head"] = sectionHeadView;
        if(sectionFootView) infos[@"foot"] = sectionFootView;
        infos[@"cells"] = cells;
        [sections addObject:infos];
    }
    self.listView.listHeaderView = nil;
    self.listView.listFooterView = nil;
    [self.listView deleteAllSections];
    //
    [self __resetView:headView];
    self.listView.listHeaderView = footView;
    
    for(int i=0;i<sections.count;i++){
        NSDictionary *infos = sections[sections.count-1-i];
        UIView *sectionHeadView = infos[@"head"];
        UIView *sectionFootView = infos[@"foot"];
        NSArray *cells = infos[@"cells"];
        [self.listView addSection];
        [self __resetView:sectionFootView];
        [self.listView setSectionView:sectionFootView atIndex:i kind:(MKUIListViewSectionViewKindOfHead)];
        
        for(int j=0;j<cells.count;j++){
            UIView *cell = cells[cells.count-j-1];
            [self __resetView:cell];
            [self.listView insertItem:cell atIndexPath:[NSIndexPath indexPathForItem:j inSection:i]];
        }
        [self __resetView:sectionHeadView];
        [self.listView setSectionView:sectionHeadView atIndex:i kind:(MKUIListViewSectionViewKindOfFoot)];
    }
    [self __resetView:footView];
    self.listView.listFooterView = headView;
    [self.listView reloadDataWithAnimation:(MKUIListViewItemAnimationAutomatic)];
}
- (void)__resetView:(UIView *)view{
    if(!view)return;
    if([view isKindOfClass:[TestListViewTextCellView class]]){
        TestListViewTextCellView *c = (TestListViewTextCellView *)view;
        c.expend = NO;
    }
    if([view isKindOfClass:[MKUIListSelectionCellViewBase class]]){
        MKUIListSelectionCellViewBase *cell = (MKUIListSelectionCellViewBase *)view;
        if(cell.enableSelection){
            [cell setSelected:NO animated:YES];
        }
    }
}
- (void)__onRefresh{
//    [self.listView reloadData];
//    [self.listView reloadData];
    [self.listView performBatchUpdates:^{
        UIView *v1 = [self.listView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
        UIView *v2 = [self.listView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
        if(v1 && v2){
            [self.listView reloadItem:v1 atIndexPath:[NSIndexPath indexPathForRow:1 inSection:2] withAnimation:(MKUIListViewItemAnimationAutomatic)];
            [self.listView reloadItem:v2 atIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
    } animated:YES completion:^(BOOL finished) {
//        [self.listView reloadData];
    }];
//    [self.listView reloadDataWithAnimated:YES completion:^(BOOL finished) {
//
//    }];
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [self.listView willTransitionToSizeChange];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.safeBounds;
//    CGRect f1 = bounds;
//    CGSize s1 = [self.listView sizeThatFits:CGSizeMake(bounds.size.width, 99999999)];
//    f1.size.width = s1.width;
//    f1.size.height = MIN(bounds.size.height,s1.height);
//    MKCGRectAlignMidXToRect(&f1, bounds);
//    self.listView.frame = f1;
    self.listView.frame = self.view.bounds;
    
    
    CGRect f2 = bounds;
    f2.size.height = 1;
    UIEdgeInsets insets = self.listView.mk_adjustedContentInset;
    UIEdgeInsetsInsetRect(self.listView.frame, insets);
    f2.origin.y = CGRectGetMidY(UIEdgeInsetsInsetRect(self.listView.frame, insets));
//    f2.origin.y = self.listView.frame.origin.y+self.listView.mk_adjustedContentInset.top+(self.listView.frame.size.height-MKUIEdgeInsetsGetEdgeSum(self.listView.mk_adjustedContentInset, MKCGAxisY)-f2.size.height)/2;
    self.lineView.frame = f2;
}
- (TestListViewTextCellView *)createCellView:(NSString *)title{
    TestListViewTextCellView *cell = [[TestListViewTextCellView alloc] initWithTitle:[NSString stringWithFormat:@"%@1234567890987654321234567890987654321\n%@,%@",title,title,title]];
    @MK_WEAKIFY(self);
    [cell addButtonWithTitle:@"添加" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        TestListViewTextCellView *newCell = [self createCellView:[NSString stringWithFormat:@"%@ 副本",title]];
        NSIndexPath *indexPath = [self.listView indexPathForCell:cellView];
        [[self.listView sectionModelAtIndex:indexPath.section] insertItem:newCell atIndex:indexPath.item withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"替换" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        TestListViewTextCellView *newCell = [self createCellView:[NSString stringWithFormat:@"%@ 副本",title]];
        NSIndexPath *indexPath = [self.listView indexPathForCell:cellView];
        [[self.listView sectionModelAtIndex:indexPath.section] reloadItem:newCell atIndex:indexPath.item withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"删除" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        NSIndexPath *indexPath = [self.listView indexPathForCell:cellView];
        [[self.listView sectionModelAtIndex:indexPath.section] deleteItem:cellView withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"居上" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        [self.listView scrollToItemAtIndexPath:[self.listView indexPathForCell:cellView] atScrollPosition:(MKUIListViewScrollPositionHead) animated:YES completion:^(BOOL finished) {
            
        }];
    }];
    [cell addButtonWithTitle:@"居中" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        [self.listView scrollToItemAtIndexPath:[self.listView indexPathForCell:cellView] atScrollPosition:(MKUIListViewScrollPositionMiddle) animated:YES completion:^(BOOL finished) {
            
        }];
    }];
    [cell addButtonWithTitle:@"居下" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        [self.listView scrollToItemAtIndexPath:[self.listView indexPathForCell:cellView] atScrollPosition:(MKUIListViewScrollPositionFoot) animated:YES completion:^(BOOL finished) {
            
        }];
    }];
    [cell addButtonWithTitle:@"展开" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        cellView.expend = !cellView.expend;
        [button setTitle:@"收起" forState:(UIControlStateSelected)];
        button.selected = cellView.expend;
        CGRect f1 = cellView.frame;
        f1.size = [cellView sizeThatFits:CGSizeMake(f1.size.width, 9999)];
        [UIView animateWithDuration:0.25 animations:^{
            cellView.frame = f1;
            [self.listView reloadItem:cellView withAnimation:(MKUIListViewItemAnimationNone)];
        }];
    }];
    [cell addButtonWithTitle:@"上移" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        NSIndexPath *fromIndexPath = [self.listView indexPathForCell:cellView];
        NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:fromIndexPath.item-1 inSection:fromIndexPath.section];
        if(fromIndexPath.item==0){
            NSInteger cellCount = [self.listView numberOfItemsInSection:fromIndexPath.section-1];
            if(cellCount>0){
                toIndexPath = [NSIndexPath indexPathForItem:cellCount inSection:fromIndexPath.section-1];
            }
        }
        [self.listView moveItemFromIndexPath:fromIndexPath to:toIndexPath withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"下移" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        NSIndexPath *fromIndexPath = [self.listView indexPathForCell:cellView];
        NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:fromIndexPath.item+1 inSection:fromIndexPath.section];
        if(fromIndexPath.item==[self.listView numberOfItemsInSection:fromIndexPath.section]-1){
            toIndexPath = [NSIndexPath indexPathForItem:0 inSection:fromIndexPath.section+1];
        }
        [self.listView moveItemFromIndexPath:fromIndexPath to:toIndexPath withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"选中" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        [cellView setSelected:!cellView.selected animated:YES];
    }];
    return cell;
}
- (TestListViewTextCellView *)createSectionView:(NSString *)title{
    TestListViewTextCellView *cell = [[TestListViewTextCellView alloc] initWithTitle:[NSString stringWithFormat:@"%@,%@,%@",title,title,title]];
    cell.showBorder = YES;
    cell.backgroundColor = [UIColor lightGrayColor];
    @MK_WEAKIFY(self);
    [cell addButtonWithTitle:@"添加Cell" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        MKUIListViewSectionViewKind kind;
        NSInteger sectionIndex = [self.listView indexForSectionView:cellView kind:&kind];
        NSIndexPath *indexPath = kind==MKUIListViewSectionViewKindOfHead?[NSIndexPath indexPathForItem:0 inSection:sectionIndex]:[NSIndexPath indexPathForItem:[self.listView numberOfItemsInSection:sectionIndex] inSection:sectionIndex];
        TestListViewTextCellView *newCell = [self createCellView:[NSString stringWithFormat:@"This is Row:%@-%@ 副本",@(sectionIndex),@(indexPath.item)]];
        [self.listView insertItem:newCell atIndexPath:indexPath withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"添加分组" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        TestListViewTextCellView *newCell = [self createSectionView:[NSString stringWithFormat:@"%@ 副本",title]];
        MKUIListViewSectionViewKind kind;
        NSInteger sectionIndex = [self.listView indexForSectionView:cellView kind:&kind];
        NSInteger newSectionIndex = kind==MKUIListViewSectionViewKindOfHead?sectionIndex:sectionIndex+1;
        [self.listView insertSectionAtIndex:newSectionIndex];
        [self.listView setSectionView:newCell atIndex:newSectionIndex kind:kind withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"删除" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        [self.listView deleteSectionView:cellView withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"删除整组" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        NSInteger sectionIndex = [self.listView indexForSectionView:cellView kind:nil];
        [self.listView deleteSectionAtIndex:sectionIndex withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    [cell addButtonWithTitle:@"展开" action:^(TestListViewTextCellView *cellView,MKUIFlowLayoutButton *button) {
        @MK_NORMALIZE(self);
        cellView.expend = !cellView.expend;
        [button setTitle:@"收起" forState:(UIControlStateSelected)];
        button.selected = cellView.expend;
        MKUIListViewSectionViewKind kind;
        NSInteger sectionIndex = [self.listView indexForSectionView:cellView kind:&kind];
        [self.listView reloadSectionView:cellView atIndex:sectionIndex kind:kind withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }];
    return cell;
}
- (void)__reloadData{
    {
        self.listView.listHeaderView = self.listHeadView;
        self.listView.listFooterView = self.listFootView;
    }
    
    {
        NSInteger sectionIndex = 0;
        [self.listView addSection];
        {
            TestListExpendCell *expendCell = [[TestListExpendCell alloc] init];
            expendCell.menus = [Menu sharedMenusWithCount:4];
            [expendCell reloadData];
            [self.listView setSectionView:expendCell atIndex:sectionIndex kind:(MKUIListViewSectionViewKindOfHead) withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        {
            [self.listView addItem:[self createCellView:@"This is Row"] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        {
            TestListExpendCell *expendCell = [[TestListExpendCell alloc] init];
            expendCell.menus = [Menu sharedMenusWithCount:3];
            [expendCell reloadData];
            [self.listView addItem:expendCell withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        
        sectionIndex++;
        [self.listView addSectionWithHeadView:[self createSectionView:[NSString stringWithFormat:@"This is secion head:%@",@(sectionIndex)]] footView:nil withAnimation:(MKUIListViewItemAnimationAutomatic)];
        for (int i=0; i<2; i++) {
            TestListViewTextCellView *cell = [self createCellView:[NSString stringWithFormat:@"This is Row:%@-%@",@(sectionIndex),@(i)]];
            cell.selectedBackgroundColor = [UIColor mk_colorWithLight:[UIColor blueColor]];
            cell.highlightedBackgroundColor = [UIColor mk_colorWithLight:[UIColor redColor]];
            [self.listView addItem:cell withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        
        sectionIndex++;
        [self.listView addSectionWithHeadView:[self createSectionView:[NSString stringWithFormat:@"This is secion head:%@",@(sectionIndex)]] footView:[self createSectionView:[NSString stringWithFormat:@"This is secion foot:%@",@(sectionIndex)]] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        for (int i=0; i<5; i++) {
            [self.listView addItem:[self createCellView:[NSString stringWithFormat:@"This is Row:%@-%@",@(sectionIndex),@(i)]] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        
        sectionIndex++;
        [self.listView addSectionWithHeadView:nil footView:[self createSectionView:[NSString stringWithFormat:@"This is secion foot:%@",@(sectionIndex)]] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        for (int i=0; i<10; i++) {
            [self.listView addItem:[self createCellView:[NSString stringWithFormat:@"This is Row:%@-%@",@(sectionIndex),@(i)]] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }
        sectionIndex++;
        [self.listView addSectionWithHeadView:nil footView:nil withAnimation:(MKUIListViewItemAnimationAutomatic)];
    }
    [self.listView reloadData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.listView deselectAllItemsWithAnimated:YES];
}
@end
